package day004;

import java.util.LinkedList;
import java.util.Queue;

public class CodedLock {
    public static void main(String[] args) {

        BFS("0000");
    }

    static String plusOne(String s, int j) {
        char[] ch = s.toCharArray();
        if (ch[j] == '9')
            ch[j] = '0';
        else
            ch[j] += 1;
        return new String(ch);
    }

    static String minusOne(String s, int j) {
        char[] ch = s.toCharArray();
        if (ch[j] == '0')
            ch[j] = '9';
        else
            ch[j] -= 1;
        return new String(ch);
    }

    static void BFS(String target) {
        Queue<String> q = new LinkedList<>();
        q.offer("0000");
        while (!q.isEmpty()) {
            final int sz = q.size();

            for (int i = 0; i < sz; i++) {
                //Retrieves and removes the head of this queue, or returns null if this queue is empty.
                final String cur = q.poll();
                System.out.println(cur);


                for (int j = 0; j < 4; j++) {
                    final String plusOne = plusOne(cur, j);
                    final String down = minusOne(cur, j);
                    q.offer(plusOne);
                    q.offer(down);
                }
            }
        }
    }
}
